package cn.lyjuan.pojoutils.template.impl;

import cn.lyjuan.pojoutils.bean.ColumnBean;
import cn.lyjuan.pojoutils.bean.TableBean;
import cn.lyjuan.pojoutils.cst.BaseCst;
import cn.lyjuan.pojoutils.template.IMapperTemplate;
import cn.lyjuan.pojoutils.util.PojoUtils;

import java.util.List;

/**
 * 生成 Mapper 接口的模版适配器，一般情况下这个模版足够使用
 * Created by ly on 2014/11/21.
 */
public class AMapperTemplateImpl implements IMapperTemplate
{
    @Override
    public String genMapperInterface(TableBean tableBean, List<ColumnBean> cols)
    {
        StringBuilder sb = new StringBuilder()
        .append("package ").append(tableBean.getMapperPkgName()).append(";\n\n");
        if (!tableBean.getMapperPkgName().equals(tableBean.getPojoPkgName()))// 如果mapper与pojo同一个包下，则无需导入POJO
            sb.append("import ").append(tableBean.getPojoPkgName() + "." + tableBean.getPojoName() + ";\n");
        sb.append("import org.apache.ibatis.annotations.Param;\n")
        .append("\n")
        .append("import java.util.List;\n \n")
        .append("/**\n")
        .append(" * " + BaseCst.CREATE_INFO).append("\n */\n")
        .append("public interface ").append(tableBean.getMapperName()).append("\n")
        .append("{\n");

        // 查询方法
        sb.append(genSelect(tableBean, cols)).append("\n");
        // 添加方法
        sb.append(genInsert(tableBean, cols)).append("\n");
        // 更新方法
        sb.append(genUpdate(tableBean, cols)).append("\n");
        // 删除方法
        sb.append(genDelete(tableBean, cols)).append("\n");

        sb.append("\n}");

        return sb.toString();
    }

    @Override
    public String genSelect(TableBean tableBean, List<ColumnBean> cols)
    {
        StringBuilder sb = new StringBuilder()
        // 普通单个查询
        .append("\t").append(tableBean.getPojoName()).append(" select(@Param(\"").append(tableBean.getObjName()).append("\")")
                .append(tableBean.getPojoName()).append(" ").append(tableBean.getObjName()).append(");\n\n");
        for (ColumnBean c : cols)// 主键查询
        {
            if (c.isPriKey)
            {
                sb.append("\t").append(tableBean.getPojoName()).append(" selectBy").append(PojoUtils.firstUpLowCase(c.column, true))
                        .append("(@Param(\"").append(c.column).append("\")").append(PojoUtils.typeCaseForClass(c.typeName.toLowerCase())).append(" ").append(c.column).append(");\n\n");
                break;// 只需要一个主键
            }
        }
        // 列表查询
        sb.append("\tList<").append(tableBean.getPojoName()).append("> selects(@Param(\"").append(tableBean.getObjName())
                .append("\")").append(tableBean.getPojoName() + " " + tableBean.getObjName()).append(");\n\n")
        // 分页查询
        .append("\tList<").append(tableBean.getPojoName()).append("> selectForPage(@Param(\"").append(tableBean.getObjName())
                .append("\")").append(tableBean.getPojoName() + " " + tableBean.getObjName()).append(", @Param(\"start\")int start, @Param(\"len\")int len);\n");

        return sb.toString();
    }

    @Override
    public String genInsert(TableBean tableBean, List<ColumnBean> cols)
    {
        return "\tint insert(@Param(\"" + tableBean.getObjName() + "\")" + tableBean.getPojoName() + " " + tableBean.getObjName() + ");\n";
    }

    @Override
    public String genUpdate(TableBean tableBean, List<ColumnBean> cols)
    {
        for (ColumnBean c : cols)
        {
            if (c.isPriKey)
                return "\tint updateBy" + PojoUtils.firstUpLowCase(c.column, true) + "(@Param(\""
                        + tableBean.getObjName() + "\")" + tableBean.getPojoName() + " " + tableBean.getObjName() + ");\n";
        }

        return "";
    }

    @Override
    public String genDelete(TableBean tableBean, List<ColumnBean> cols)
    {
        String str = "";

        for (ColumnBean c : cols)
        {
            if (c.isPriKey)
            {
                str += "\tint deleteBy" + PojoUtils.firstUpLowCase(c.column, true) + "(@Param(\"" + c.column + "\")"
                        + PojoUtils.typeCaseForClass(c.typeName.toLowerCase()) + " " + c.column + ");\n\n";
                break;
            }
        }

        /**
         * @Modify by Chad@2016-03-08
         * @Desc 去掉SQL语句关联数据库名称
         */
//        str += "\tint delete(@Param(\"" + tableBean.getObjName() + "\")" + tableBean.getPojoName() + " " + tableBean.getObjName() + ");\n";

        return str;
    }
}
